package org.mebusw.remote;

import java.util.List;
import org.mebusw.dao.IGameTableDAO;
import org.mebusw.dao.factory.DAOFactory;
import org.mebusw.game.IGameManager;
import org.mebusw.game.IGameStatus;
import org.mebusw.vo.GameBrief;
import org.mebusw.vo.GameTable;

import flex.messaging.MessageBroker;
import flex.messaging.messages.AsyncMessage;
import flex.messaging.util.UUIDUtils;

public class GamePlayService {
	public GamePlayService() {
		System.out.println("GamePlayService.GamePlayService()");
	}

	public int createGame(String playerName, int uid, int playerCount,
			String description, String gameName, int expansion) {
		System.out.println("GamePlayService.createGame:" + playerCount
				+ description + gameName + expansion);
		IGameManager gm = null;
		if (gameName.equals(GameTable.DAVINCI_CODE)) {
			gm = new org.mebusw.davincicode.GameManager();
		} else if (gameName.equals(GameTable.LOST_CITIES)) {
			gm = new org.mebusw.lostcities.GameManager();
		}

		gm.createGame(playerName, uid, playerCount, description, gameName,
				expansion);
		if (gm.getGt().getState() == IGameStatus.NOTSTARTED) {
			gm.joinGame(playerName, uid);
			gm.saveGame();
		}
		return gm.getGt().getGid();
	}

	public boolean joinGame(String playerName, int uid, int gid, String gameName) {
		System.out.println("GamePlayService.joinGame: " + playerName + gid
				+ gameName);
		IGameManager gm = null;
		if (gameName.equals(GameTable.DAVINCI_CODE)) {
			gm = new org.mebusw.davincicode.GameManager();
		} else if (gameName.equals(GameTable.LOST_CITIES)) {
			gm = new org.mebusw.lostcities.GameManager();
		}

		gm.loadGame(gid);
		if (gm.getGt().getState() == IGameStatus.NOTSTARTED) {
			gm.joinGame(playerName, uid);
			gm.saveGame();
			return true;
		}
		return false;
	}

	protected void refreshAllUI(Object body, String from, int gid, String to) {
		MessageBroker msgBroker = MessageBroker.getMessageBroker(null);
		String clientID = UUIDUtils.createUUID();
		AsyncMessage msg = new AsyncMessage();
		msg.setDestination("gameroom-play");
		msg.setHeader("type", "game"); // -game -supervision
		msg.setHeader("scope", "gameroom"); // -gameroom -gamehall -all -private
		msg.setHeader("from", from);
		msg.setHeader("to", to);
		msg.setHeader("gid", Integer.valueOf(gid));
		msg.setClientId(clientID);
		msg.setMessageId(UUIDUtils.createUUID());
		msg.setTimestamp(System.currentTimeMillis());
		msg.setBody(body);
		msgBroker.routeMessageToService(msg, null);
		System.out.println("server sends msg FROM " + from + " TO " + to
				+ " WITH " + body);
	}

	public List<GameBrief> getGameList() {
		System.out.println("getGameList on server");
		IGameTableDAO dao = DAOFactory.getIGameTableDAOInstance();
		List<GameBrief> gameList = null;
		try {
			gameList = dao.findAll();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return gameList;
	}

	protected int refreshGame(String playerName, int gid, String gameName) {
		System.out.println("GamePlayService.refreshGame: " + playerName + gid
				+ gameName);
		IGameManager gm = null;
		if (gameName.equals(GameTable.DAVINCI_CODE)) {
			gm = new org.mebusw.davincicode.GameManager();
		} else if (gameName.equals(GameTable.LOST_CITIES)) {
			gm = new org.mebusw.lostcities.GameManager();
		}

		gm.loadGame(gid);
		refreshAllUI(gm.getGtVO(), playerName, gid, playerName);
		return gm.getGt().getGid();

	}

}
